﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using WPF_Dialogs.Custom_Controls;

namespace WPF_Dialogs.Dialogs
{
    /// <summary>
    /// Interaction logic for Renamer.xaml
    /// </summary>
    public partial class RenamerDialog : Window, IDialog
    {
        /// <summary>
        /// Gets or sets the dialog result value, which is the value that is returned from the <see cref="M:System.Windows.Window.ShowDialog"/> method.
        /// </summary>
        /// <value></value>
        /// <returns>A <see cref="T:System.Nullable`1"/> value of type <see cref="T:System.Boolean"/>. The default is false.</returns>
        /// <exception cref="T:System.InvalidOperationException">
        /// 	<see cref="P:System.Windows.Window.DialogResult"/> is set before a window is opened by calling <see cref="M:System.Windows.Window.ShowDialog"/>, or a window opened by calling <see cref="M:System.Windows.Window.Show"/>.</exception>
        public new EDialogResult DialogResult { set; get; }
        /// <summary>
        /// Gets or sets a value indicating whether the new object should be a folder or a file. DEFAULT: True
        /// </summary>
        /// <value><c>true</c> if the new object should be a folder; otherwise, <c>false</c>.</value>
        public bool IsFolder { get; set; }
        private string oldname;

        public RenamerDialog(string oldname)
        {
            InitializeComponent();
            this.textBoxFolderName.Focus();
            this.oldname = oldname;
            this.IsFolder = true;
        }

        #region public functions
        public EDialogResult showDialog()
        {
            this.textBoxFolderName.SelectAll();
            this.ShowDialog();
            return this.DialogResult;
        }
        #endregion

        #region public Properties
        /// <summary>
        /// Gets or sets the name of the defaultfoldername.
        /// </summary>
        /// <value>The name of the folder.</value>
        public string FolderName
        {
            get
            {
                return this.textBoxFolderName.Text;
            }
            set
            {
                this.textBoxFolderName.Text = value;
            }
        }

        /// <summary>
        /// Sets the content of the label text.
        /// </summary>
        /// <value>The content of the label text.</value>
        public object LabelTextContent
        {
            set
            {
                this.labelText.Content = value;
            }
        }

        /// <summary>
        /// Sets the content of the button OK.
        /// </summary>
        /// <value>The content of the button OK.</value>
        public object ButtonOKContent
        {
            set
            {
                this.buttonOK.Content = value;
            }
        }

        /// <summary>
        /// Sets the content of the button cancel.
        /// </summary>
        /// <value>The content of the button cancel.</value>
        public object ButtonCancelContent
        {
            set
            {
                this.buttonCancel.Content = value;
            }
        }
        #endregion

        #region private functions
        private void DialogButton_clicked(object sender, RoutedEventArgs e)
        {
            DialogButton db = e.OriginalSource as DialogButton;
            this.DialogResult = db.DialogResult;
            if (this.DialogResult == EDialogResult.OK)
                rename();
            this.Close();
            e.Handled = true;
        }

        private new bool? ShowDialog()
        {
            return base.ShowDialog();
        }

        private void textBoxFolderName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Return)
            {
                this.DialogResult = EDialogResult.OK;
                rename();
                this.Close();    
            }
            e.Handled = true;
        }

        private void rename()
        {
            string path = this.oldname.Remove(this.oldname.LastIndexOf("\\") + 1);
            try
            {
                if (IsFolder)
                {
                    if (Directory.Exists(path + this.FolderName))
                    {
                        IOException ex = new IOException("There is a Folder with this name in this directory!");
                        throw ex;
                    }
                    else
                    {
                        DirectoryInfo di = new DirectoryInfo(this.oldname);
                        di.MoveTo(path + this.FolderName);
                    }
                }
                else
                {
                    string type = this.oldname.Remove(0, this.oldname.LastIndexOf("."));
                    if (File.Exists(this.oldname + this.FolderName + type))
                    {
                        IOException ex = new IOException("There is a File with this name in this directory!");
                        throw ex;
                    }
                    else
                    {
                        FileInfo fi = new FileInfo(this.oldname);
                        fi.MoveTo(path + this.FolderName);
                    }
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
